<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">{{title}}</h3>
      <el-form-item prop="staffNo">
        <el-input v-model="loginForm.staffNo" type="text" auto-complete="off" placeholder="员工编号">
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
                  @keyup.enter.native="handleLogin">
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2024 发发发全都发有限公司 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import {login} from "../api/staff.js";
import {ElMessage} from "element-plus";
import MD5 from 'md5'

export default {
  name: "StaffLogin",
  data() {
    return {
      title: "员工管理系统",
      loginForm: {
        staffNo: "",
        password: "",
      },
      loginRules: {
        staffNo: [
          { required: true, trigger: "blur", message: "请输入员工编号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" }
        ]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    async handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) {
          return;
        }

        this.loading = true;
        try {
          // 密码加密（与原代码相同逻辑）
          const loginData = {
            ...this.loginForm,
            password: MD5(this.loginForm.password)
          };

          let {data: data} = await login(loginData);

          if (data.code === 1000) {
            ElMessage.success(data.msg);
            // 存储token（与原代码相同逻辑）
            sessionStorage.setItem("token", data.obj);
            // 跳转页面
            this.$router.push(this.redirect || '/index');
          } else {
            ElMessage.error(data.msg);
          }
        } catch (error) {
          console.error("登录失败:", error);
          ElMessage.error("登录失败，请稍后重试");
        } finally {
          this.loading = false;
        }
      });
    }
  }
}
</script>

<style scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-width: 1280px;
  min-height: 100vh;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  z-index: 1;
}

.login-form .el-input {
  height: 38px;
}

.login-form .el-input input {
  height: 38px;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>